package com.jacoblyu.factoryrunner.ui.views

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.icons.filled.Home
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.compose.ui.platform.LocalContext
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.remember
import com.jacoblyu.factoryrunner.data.TestResultManager
import com.jacoblyu.factoryrunner.ui.theme.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun ScreenTestView(navController: NavController) {
    // 根据屏幕尺寸调整字体和间距
    val screenWidth = LocalConfiguration.current.screenWidthDp
    val screenHeight = LocalConfiguration.current.screenHeightDp
    
    // 根据屏幕尺寸调整字体大小
    val titleFontSize = if (screenWidth >= 1920) 48.sp else 32.sp
    val contentFontSize = if (screenWidth >= 1920) 36.sp else 24.sp
    val buttonFontSize = if (screenWidth >= 1920) 28.sp else 18.sp
    
    // 根据屏幕尺寸调整间距
    val spacing = if (screenWidth >= 1920) 32.dp else 24.dp
    val buttonHeight = if (screenWidth >= 1920) 80.dp else 60.dp
    val buttonWidth = if (screenWidth >= 1920) 200.dp else 150.dp
    
    // 初始化TestResultManager
    val context = LocalContext.current
    val testResultManager = remember { TestResultManager(context) }
    
    // 屏幕测试状态
    var screenTestResult by remember { mutableStateOf(testResultManager.getTestResult("屏幕测试")) }
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        // 标题栏
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(spacing),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Home按钮
            IconButton(
                onClick = { navController.navigate("home") }
            ) {
                Icon(
                    imageVector = Icons.Filled.Home,
                    contentDescription = "返回主页",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
            
            // 标题
            Text(
                text = "屏幕测试",
                fontSize = titleFontSize,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            // 下一步按钮
            IconButton(
                onClick = { navController.navigate("audio_test") }
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "下一步",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
        }
        
        // 内容区域
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(spacing),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 屏幕测试内容
            Card(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .height((screenHeight * 0.4f).dp),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface
                ),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = 8.dp
                )
            ) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "屏幕测试",
                            fontSize = contentFontSize,
                            fontWeight = FontWeight.Bold,
                            color = MaterialTheme.colorScheme.onSurface,
                            textAlign = TextAlign.Center
                        )
                        
                        Spacer(modifier = Modifier.height(spacing))
                        
                        Text(
                            text = "请检查屏幕显示是否正常",
                            fontSize = contentFontSize,
                            color = MaterialTheme.colorScheme.onSurface,
                            textAlign = TextAlign.Center
                        )
                        
                        Spacer(modifier = Modifier.height(spacing))
                        
                        // 彩色条用于测试屏幕颜色
                        Row(
                            modifier = Modifier.fillMaxWidth(0.8f),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            Box(
                                modifier = Modifier
                                    .size(60.dp)
                                    .background(Color.Red)
                            )
                            Box(
                                modifier = Modifier
                                    .size(60.dp)
                                    .background(Color.Green)
                            )
                            Box(
                                modifier = Modifier
                                    .size(60.dp)
                                    .background(Color.Blue)
                            )
                        }
                    }
                }
            }
            
            Spacer(modifier = Modifier.height(spacing))
            
            // 按钮区域
            Row(
                horizontalArrangement = Arrangement.spacedBy(spacing),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 不通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("屏幕测试", "失败")
                        screenTestResult = "失败"
                        // 导航到下一个测试页面
                        navController.navigate("audio_test?result=失败&test=屏幕测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error
                    )
                ) {
                    Text(
                        text = "不通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onError
                    )
                }
                
                // 通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("屏幕测试", "通过")
                        screenTestResult = "通过"
                        // 导航到下一个测试页面
                        navController.navigate("audio_test?result=通过&test=屏幕测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary
                    )
                ) {
                    Text(
                        text = "通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onPrimary
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun ScreenTestPreview() {
    FactoryrunnerTheme {
        ScreenTestView(rememberNavController())
    }
}

@Preview(showBackground = true, widthDp = 1920, heightDp = 1200)
@Composable
fun ScreenTestLargePreview() {
    FactoryrunnerTheme {
        ScreenTestView(rememberNavController())
    }
}